package com.yueqian.framework.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMsgRecoverConfig {
	/*
	 * 声明消息的恢复策略
	 * */
	@Bean
	public MessageRecoverer republishMsgRecover(RabbitTemplate rabbitTemplate) {
		return new RepublishMessageRecoverer(rabbitTemplate, "error.direct.exchange", "error");
	}

	/*
	 * 声明交换机
	 * */
	@Bean
	public TopicExchange errorExchange() {
		return ExchangeBuilder.topicExchange("error.direct.exchange").build();
	}


	/*
	 * 声明队列
	 * */
	@Bean
	public Queue errorQueue() {
		return QueueBuilder.durable("error_queue").build();
	}

	/*
	 * 声明绑定关系
	 * */
	@Bean
	public Binding errorQueueBinding(Queue errorQueue, TopicExchange errorExchange) {
		return BindingBuilder.bind(errorQueue).to(errorExchange).with("error");
	}

}
